package com.harvest.scientific.ladder.page.fa

import android.util.Log
import com.harvest.scientific.ladder.base.BaseMVIViewModel
import com.harvest.scientific.ladder.base.IUiIntent
import com.harvest.scientific.ladder.base.IUiState
import com.harvest.scientific.ladder.net.ReRsitory
import com.harvest.scientific.ladder.net.request
import com.harvest.scientific.ladder.util.LogUtils
import com.harvest.scientific.ladder.util.SpUtil

sealed class MineUiState : IUiState {
    data class GetIdentificationState(val spokenHeadacheScientificDarknessEveryBottom:String) : MineUiState()

    data class GetUserInfoState(val name1: String, val name2: String) : MineUiState()
}

sealed class MineIntent: IUiIntent {
    object GetIdentificationIntent : MineIntent()

}


/**
 * @author：zenglingwen
 * @date：2024-06-10 12:37
 * @desc：
 */
class MineViewModel : BaseMVIViewModel<MineIntent, MineUiState>() {
    val repository  = ReRsitory()
    override fun initUiState(): MineUiState {
        return MineUiState.GetIdentificationState("")
    }

    override fun handleIntent(intent: MineIntent) {
        when(intent) {
            is MineIntent.GetIdentificationIntent -> {

                requestIdentification()
            }
        }
    }

    private fun requestIdentification() {
        request(
            requestBlock = {
                repository.admireMoreAche("4")
            },

            callBack = {
                onSuccess = { res->
                   LogUtils.d("requestIdentification success:$res")

                    res?.let {
                        updateUiState {
                            MineUiState.GetIdentificationState(res.spokenHeadacheScientificDarknessEveryBottom ?: "")
                        }
                    }
                }
                onFailed = { code, codeStr ->
                    LogUtils.d("requestIdentification error $code, $codeStr")
                }
            }

        )
    }

    fun fleeSocialistExpedition(
        mexicanRegardsMistakenRoofInterestingGallery:Int
    ){
        request(requestBlock = {
            repository.fleeSocialistExpedition(mexicanRegardsMistakenRoofInterestingGallery)
        }, callBack = {
            onSuccess = {
                if (mexicanRegardsMistakenRoofInterestingGallery == 1) {
                    SpUtil.put(SpUtil.KYE_USER_NAME_ONE,it?.festivalPaper ?: "")
                    SpUtil.put(SpUtil.KYE_USER_NAME_ONE,it?.suddenGasTwin ?: "")
                    updateUiState {
                        MineUiState.GetUserInfoState(it?.festivalPaper ?: "", it?.suddenGasTwin ?: "")
                    }

                }
            }
            onFailed = { code, msg ->
                if (mexicanRegardsMistakenRoofInterestingGallery == 1) {
                    updateUiState {
                        MineUiState.GetUserInfoState(SpUtil.getString(SpUtil.KYE_USER_NAME_ONE,""), SpUtil.getString(SpUtil.KYE_USER_NAME_TWO,""))
                    }
                }
            }
        })
    }

}